home *** CD-ROM | disk | FTP | other *** search
- /*
- File: TriPatch.c
-
- Used to build: “Fractal 7”
-
- Written by: Dan Clifford November 1994
-
- Description:
- The following code implements a patch-shading algorithm
- for doing surface shading.
- */
-
-
- #include "Fractal.h"
-
- unsigned char *gBlitData = NULL;
- Rect gBlitBounds = { 0, 0, 0, 0 };
- short gBlitRowBytes = 0;
-
-
- /*
- SetBlitBuffer
-
- Call this function to specify the blitting buffer as well as the
- bounds and the number of bytes per row.
- */
- void SetBlitBuffer(unsigned char *blitData, Rect *blitBounds, short blitRowBytes)
- {
- gBlitData = blitData;
- gBlitRowBytes = blitRowBytes;
- gBlitBounds = *blitBounds;
- }
-
-
- /*
- FillTriangle
-
- This function fills in a triangular patch of pixels. It assumes the offscreen
- buffer is an 8-byte buffer, and fills in each pixel with the specified color.
- */
- void FillTriangle(long vertex1H, long vertex1V, long vertex2H, long vertex2V,
- long vertex3H, long vertex3V, unsigned char color)
- {
- if (vertex2V < vertex1V) {
- long temp;
-
- temp = vertex2V;
- vertex2V = vertex1V;
- vertex1V = temp;
-
- temp = vertex2H;
- vertex2H = vertex1H;
- vertex1H = temp;
- }
-
- if (vertex3V < vertex1V) {
- long temp;
-
- temp = vertex3V;
- vertex3V = vertex1V;
- vertex1V = temp;
-
- temp = vertex3H;
- vertex3H = vertex1H;
- vertex1H = temp;
- }
-
- if (vertex3H < vertex2H) {
- long temp;
-
- temp = vertex3V;
- vertex3V = vertex2V;
- vertex2V = temp;
-
- temp = vertex3H;
- vertex3H = vertex2H;
- vertex2H = temp;
- }
-
- if (vertex3V > vertex2V) {
- long currentLeftH,
- currentRightH,
- currentLeftIncrement,
- currentRightIncrement;
- long currentV;
- unsigned char *currentPixel;
-
- currentLeftH = (long)(vertex1H) << 16;
- currentRightH = currentLeftH;
-
- currentLeftIncrement = ((long)(vertex2H - vertex1H) << 16) / (vertex2V - vertex1V);
- currentRightIncrement = ((long)(vertex3H - vertex1H) << 16) / (vertex3V - vertex1V);
-
- for (currentV = vertex1V; currentV < vertex2V; currentV++) {
-
- long currentRightHlong = currentRightH >> 16;
- long currentLeftHlong = currentLeftH >> 16;
- long currentH;
-
- currentPixel = gBlitData + (currentV * gBlitRowBytes) + currentLeftHlong;
-
- for (currentH = currentLeftHlong; currentH <= currentRightHlong; currentH++) {
- *currentPixel = color;
- currentPixel++;
- }
-
- currentLeftH += currentLeftIncrement;
- currentRightH += currentRightIncrement;
-
- }
-
- currentLeftH = (long)(vertex2H) << 16;
- currentLeftIncrement = ((long)(vertex3H - vertex2H) << 16) / (vertex3V - vertex2V);
-
- for (currentV = vertex2V; currentV <= vertex3V; currentV++) {
- long currentRightHlong = currentRightH >> 16;
- long currentLeftHlong = currentLeftH >> 16;
- long currentH;
-
- currentPixel = gBlitData + (currentV * gBlitRowBytes) + currentLeftHlong;
-
- for (currentH = currentLeftHlong; currentH <= currentRightHlong; currentH++) {
- *currentPixel = color;
- currentPixel++;
- }
-
- currentLeftH += currentLeftIncrement;
- currentRightH += currentRightIncrement;
- }
- }
-
- else if (vertex2V > vertex3V) {
- long currentLeftH,
- currentRightH,
- currentLeftIncrement,
- currentRightIncrement;
- long currentV;
- unsigned char *currentPixel;
-
- currentLeftH = (long)(vertex1H) << 16;
- currentRightH = currentLeftH;
-
- currentLeftIncrement = ((long)(vertex2H - vertex1H) << 16) / (vertex2V - vertex1V);
- currentRightIncrement = ((long)(vertex3H - vertex1H) << 16) / (vertex3V - vertex1V);
-
- for (currentV = vertex1V; currentV < vertex3V; currentV++) {
- long currentRightHlong = currentRightH >> 16;
- long currentLeftHlong = currentLeftH >> 16;
- long currentH;
-
- currentPixel = gBlitData + (currentV * gBlitRowBytes) + currentLeftHlong;
-
- for (currentH = currentLeftHlong; currentH <= currentRightHlong; currentH++) {
- *currentPixel = color;
- currentPixel++;
- }
-
- currentLeftH += currentLeftIncrement;
- currentRightH += currentRightIncrement;
- }
-
- currentRightH = (long)(vertex3H) << 16;
- currentRightIncrement = ((long)(vertex2H - vertex3H) << 16) / (vertex2V - vertex3V);
-
- for (currentV = vertex3V; currentV <= vertex2V; currentV++) {
- long currentRightHlong = currentRightH >> 16;
- long currentLeftHlong = currentLeftH >> 16;
- long currentH;
-
- currentPixel = gBlitData + (currentV * gBlitRowBytes) + currentLeftHlong;
-
- for (currentH = currentLeftHlong; currentH <= currentRightHlong; currentH++) {
- *currentPixel = color;
- currentPixel++;
- }
-
- currentLeftH += currentLeftIncrement;
- currentRightH += currentRightIncrement;
- }
- }
- else {
- long currentLeftH,
- currentRightH,
- currentLeftIncrement,
- currentRightIncrement;
- long currentV;
- unsigned char *currentPixel;
-
- currentLeftH = (long)(vertex1H) << 16;
- currentRightH = currentLeftH;
-
- currentLeftIncrement = ((long)(vertex2H - vertex1H) << 16) / (vertex2V - vertex1V);
- currentRightIncrement = ((long)(vertex3H - vertex1H) << 16) / (vertex3V - vertex1V);
-
- for (currentV = vertex1V; currentV < vertex3V; currentV++) {
- long currentRightHlong = currentRightH >> 16;
- long currentLeftHlong = currentLeftH >> 16;
- long currentH;
-
- currentPixel = gBlitData + (currentV * gBlitRowBytes) + currentLeftHlong;
-
- for (currentH = currentLeftHlong; currentH <= currentRightHlong; currentH++) {
- *currentPixel = color;
- currentPixel++;
- }
-
- currentLeftH += currentLeftIncrement;
- currentRightH += currentRightIncrement;
- }
- }
- }
-
-
-